/******************************************************************/
/*                                                                */
/*  Winpooch : Windows Watchdog                                   */
/*  Copyright (C) 2004-2005  Benoit Blanchon                      */
/*                                                                */
/*  This program is free software; you can redistribute it        */
/*  and/or modify it under the terms of the GNU General Public    */
/*  License as published by the Free Software Foundation; either  */
/*  version 2 of the License, or (at your option) any later       */
/*  version.                                                      */
/*                                                                */
/*  This program is distributed in the hope that it will be       */
/*  useful, but WITHOUT ANY WARRANTY; without even the implied    */
/*  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR       */
/*  PURPOSE.  See the GNU General Public License for more         */
/*  details.                                                      */
/*                                                                */
/*  You should have received a copy of the GNU General Public     */
/*  License along with this program; if not, write to the Free    */
/*  Software Foundation, Inc.,                                    */
/*  675 Mass Ave, Cambridge, MA 02139, USA.                       */
/*                                                                */
/******************************************************************/


/******************************************************************/
/* Includes                                                       */
/******************************************************************/

// module's interface
#include "ConfigDlg.h"

// project's headers 
#include "Alert.h"
#include "Config.h"
#include "Language.h"
#include "Log.h"
#include "Resources.h"
#include "Version.h"

// window's headers
#include <prsht.h>


/******************************************************************/
/* Internal macros                                                */
/******************************************************************/

#define arraysize(a) (sizeof(a)/sizeof((a)[0]))
#define clear(a) memset(&(a),0,sizeof(a))


/******************************************************************/
/* Internal functions                                             */
/******************************************************************/

BOOL CALLBACK _Config_DlgProc (HWND, UINT, WPARAM, LPARAM) ;


/******************************************************************/
/* Internal data types                                            */
/******************************************************************/

typedef struct {
  UINT		idCtrl ;
  UINT		idCfgInt ;
  UINT		idStr ;
} CONFIG_CHECKBOX ;


/******************************************************************/
/* Internal data                                                  */
/******************************************************************/

static HWND hwndConfigDlg = NULL ;

static CONFIG_CHECKBOX aCheckBoxes[] = {
  { IDC_CHECK_UPDATES,	CFGINT_CHECK_FOR_UPDATES,	_CHECK_FOR_UPDATES },
  { IDC_ENABLE_PICTURE,	CFGINT_ENABLE_POOCHY_PICTURE,	_ENABLE_PICTURE },
  { IDC_ENABLE_SOUND,	CFGINT_ENABLE_SOUND,		_ENABLE_SOUND }    
} ;


/******************************************************************/
/* Exported function : ConfigDlg_IsItsMessage                     */
/******************************************************************/

LRESULT ConfigDlg_IsItsMessage (MSG * pMsg)
{
  LRESULT lResult = PropSheet_IsDialogMessage(hwndConfigDlg, pMsg) ;

  if( lResult && ! PropSheet_GetCurrentPageHwnd(hwndConfigDlg) )
    {
      DestroyWindow (hwndConfigDlg) ;
      hwndConfigDlg = NULL ;
    }
    
  return lResult ;
}


/******************************************************************/
/* Exported function : ConfigDlg_CreateDialog                     */
/******************************************************************/

BOOL ConfigDlg_CreateDialog (HINSTANCE hInstance, HWND hwndParent)
{
  PROPSHEETHEADER	psh = {0} ;
  PROPSHEETPAGE		psp[1] = {{0}};

  if( hwndConfigDlg && IsWindow(hwndConfigDlg) ) 
    {
      SetForegroundWindow (hwndConfigDlg) ;
      return TRUE ;
    }

  //clear (psp[0]) ;
  psp[0].dwSize		= sizeof(PROPSHEETPAGE) ;
  psp[0].dwFlags	= PSP_DEFAULT | PSP_USETITLE | PSP_USEICONID ;
  psp[0].hInstance	= hInstance ;
  psp[0].pszTemplate	= MAKEINTRESOURCE(DLG_CONFIG) ;
  psp[0].pfnDlgProc	= _Config_DlgProc ;
  psp[0].pszTitle	= STR(_GENERAL) ;
  //psp[0].pszIcon = MAKEINTRESOURCE (IDI_APP) ;
  
  //clear (psh) ;
  psh.dwSize		= sizeof(PROPSHEETHEADER) ;
  psh.dwFlags		= PSH_DEFAULT|PSH_MODELESS|
    PSH_USEICONID|PSH_PROPSHEETPAGE|PSH_NOAPPLYNOW ;
  psh.hwndParent	= hwndParent ;
  psh.hInstance		= hInstance ;
  psh.nPages		= arraysize(psp) ;
  psh.ppsp		= psp ;
  psh.pszCaption	= STR(_CONFIGURATION) ;
  psh.pszIcon		= MAKEINTRESOURCE (IDI_APP) ;
  
				  
  hwndConfigDlg = (HWND) PropertySheet (&psh) ;
  
  return hwndConfigDlg!=NULL ;
}



/******************************************************************/
/* Internal function : Dlg_Proc                                   */
/******************************************************************/

BOOL CALLBACK _Config_DlgProc (HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) 
{
  TCHAR		szLanguage[MAX_LANGUAGE_NAME] ;
  NMHDR		*pnmh ;
  int		i ;

  switch( message )
    {
    case WM_INITDIALOG:
       
      // languages
      for( i=0 ; i<Language_GetLanguageCount() ; i++ )
	SendDlgItemMessage (hDlg, IDC_LANGUAGE, CB_ADDSTRING, 0,
			    (LPARAM)Language_GetLanguage(i)) ;
      
      // check boxes
      for( i=0 ; i<arraysize(aCheckBoxes) ; i++ )
	CheckDlgButton (hDlg, aCheckBoxes[i].idCtrl, 
			Config_GetInteger(aCheckBoxes[i].idCfgInt)
			? BST_CHECKED : BST_UNCHECKED) ;

      // max log size
      SetDlgItemInt (hDlg, IDC_MAX_LOG_SIZE, 
		     Config_GetInteger(CFGINT_MAX_LOG_SIZE)/1024,
		     FALSE) ;

      // set language dependant strings
      SendMessage (hDlg, WM_LANGUAGECHANGED, 0, 0) ;
      
      return TRUE ;

   case WM_LANGUAGECHANGED:

     // language
     SetDlgItemText (hDlg, IDT_LANGUAGE, STR(_LANGUAGE)) ;
     SendDlgItemMessage (hDlg, IDC_LANGUAGE, CB_SELECTSTRING, 0,
			 (LPARAM)STR(_LANGUAGE_NAME_IN_ENGLISH)) ;
     
     // check boxes
     for( i=0 ; i<arraysize(aCheckBoxes) ; i++ )
       SetDlgItemText (hDlg, aCheckBoxes[i].idCtrl, STR(aCheckBoxes[i].idStr)) ;

     // max log size
     SetDlgItemText (hDlg, IDT_MAX_LOG_SIZE, STR(_MAX_LOG_FILE_SIZE)) ;
     SetDlgItemText (hDlg, IDT_KILOBYTES, STR(_KILOBYTES)) ;
     
     return TRUE ;


    case WM_NOTIFY:

      pnmh = (NMHDR*) lParam; 

      if( pnmh->code == PSN_APPLY )
	{
	  UINT nSize ;
	  BOOL bChecked ;

	  // for each check box (begin)
	  for( i=0 ; i<arraysize(aCheckBoxes) ; i++ )
	    {
	      bChecked = IsDlgButtonChecked (hDlg, aCheckBoxes[i].idCtrl) == BST_CHECKED ;
	      
	      Config_SetInteger (aCheckBoxes[i].idCfgInt, bChecked) ;
	    }
	  // for each check box (end)
	  
	  // max log size
	  nSize = 1024 * GetDlgItemInt (hDlg, IDC_MAX_LOG_SIZE, NULL, FALSE) ;	  
	  Log_SetMaxSize (nSize) ;
	  Config_SetInteger (CFGINT_MAX_LOG_SIZE, Log_GetMaxSize()) ;

	  // language
	  GetDlgItemText (hDlg, IDC_LANGUAGE,
			  szLanguage, MAX_LANGUAGE_NAME) ;
	  if( Language_LoadByName (szLanguage) )
	    Alert_ReloadStrings () ;
	  
	  return TRUE ;
	}
      
      return FALSE ;
    }

  return FALSE ;
}
